//
//  HEBaseViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/20.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEBaseViewController.h"

@interface HEBaseViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate,UIGestureRecognizerDelegate>

// 是否显示空数据页面  默认为显示
@property(nonatomic,assign) BOOL isShowEmptyDataSet;


@end

@implementation HEBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.zx_navStatusBarStyle = ZXNavStatusBarStyleDefault;
    self.zx_navLineViewBackgroundColor = CSplitLineColor;
    
    self.view.backgroundColor = KWhiteColor;
    
    [self setupDefulat];
}

- (void)setupDefulat {
    // 空数据提示
    _emptyConfig = [HEEmptyDataModel new];
    _emptyConfig.tipsImageName = @"no_data";
    
//    self.zx_backBtnImageName = @"back_black";

}

- (void)setIsHidenNaviBar:(BOOL)isHidenNaviBar {
    self.zx_navBar.hidden = isHidenNaviBar;
}

- (void)vcBackBlock:(VCBackBlockAction)block {
    self.backBlock = block;
}

- (void)vcBackParamBlock:(HEBackBlockAction)block {
    self.backParamBlock = block;
}
 
#pragma mark - 网络检查显示类型
/**
网络检查 --> 是否显示断网提示

 viewCotroller --> 检查网络 -->有网 -->检查数据

 viewCotroller --> 检查网络 -->无网 -->无数据 --> 显示断网
                               |
                                  -->有数据 --> 弹窗提示断网
*/
/// 断网提示
- (void)setNoNetworkView {
    _emptyConfig = [HEEmptyDataModel new];
    _emptyConfig.tipsTitle = @"网络请求失败";
    _emptyConfig.tipsDetailTitle = @"请检查您的网络重新加载吧";
    _emptyConfig.btnTitle = @"重新加载";
}

#pragma mark -
- (void)customEmptyModel:(HEEmptyDataModel *)model {
    _emptyConfig = model;
}

#pragma mark -
- (void)showEmpty {
    _isShowEmptyDataSet = YES;
        
    // 先网络检查 --> 再显示空数据
    if (![WOAFNetWorkingHandle isNetwork]) {
        [self setNoNetworkView];
    }

    [self.view addSubview:self.gdTipsView];
    [_gdTipsView reloadEmptyDataSet];
}

- (void)hideEmpty {
    _isShowEmptyDataSet = NO;
    [_gdTipsView reloadEmptyDataSet];
}

//- (void)addRefresh {
//    _gdTipsView
//}

#pragma mark - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:_emptyConfig.tipsImageName];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = _emptyConfig.tipsTitle;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor grayColor]};
    
    return _emptyConfig.tipsTitle ? [[NSAttributedString alloc] initWithString:text attributes:attributes] : nil;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = _emptyConfig.tipsDetailTitle;
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return _emptyConfig.tipsDetailTitle ? [[NSAttributedString alloc] initWithString:text attributes:attributes]:nil;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -self.gdTipsView.tableHeaderView.frame.size.height/2.0f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f]};
    
    return _emptyConfig.btnTitle ? [[NSAttributedString alloc] initWithString:_emptyConfig.btnTitle attributes:attributes]  :nil;
}

- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    return _emptyConfig.btnImgName?[UIImage imageNamed:_emptyConfig.btnImgName]:nil;
}

#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

// 返回是否允许滚动，默认:YES
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return self.isShowEmptyDataSet;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self reloadRequestdata];
}
 

#pragma mark - 按钮事件
- (void)reloadRequestdata {
    
}

- (void)backBtnClicked
{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (BOOL)isVCVisable {
    if(self.isViewLoaded && self.view.window){//正在显示
        return YES;
    }
    return NO;
}

#pragma mark - getter
//TODO: 使用场景：viewController中全屏显示
- (UITableView *)gdTipsView {
    if(_gdTipsView == nil) {
        _gdTipsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStyleGrouped];
        _gdTipsView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
        _gdTipsView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
        _gdTipsView.backgroundColor = [UIColor whiteColor];
        _gdTipsView.showsVerticalScrollIndicator = NO;
        _gdTipsView.showsHorizontalScrollIndicator = NO;

        _gdTipsView.emptyDataSetSource = self;
        _gdTipsView.emptyDataSetDelegate = self;
    }

    return _gdTipsView;
}

//
- (void)setCustomTableView:(UITableView *)gdTipsView {
    _gdTipsView = gdTipsView;
    _gdTipsView.emptyDataSetSource = self;
    _gdTipsView.emptyDataSetDelegate = self;
    [_gdTipsView reloadEmptyDataSet];
}

@end


@implementation HEEmptyDataModel

@end
